const { createHash } = require('crypto');
const { apiToken } = require("../config/wechat")

const validateWechatHost = (ctx) => {
    const { signature, echostr, timestamp, nonce } = ctx.query
    console.log('打印***signature, echostr, timestamp, nonce', signature, echostr, timestamp, nonce)
    // 将token、timestamp、nonce三个参数进行字典序排序
    const stringArray = [timestamp, nonce, apiToken]
    const resultArray = stringArray.sort()
    // 将三个参数字符串拼接成一个字符串进行sha1加密
    const resultString = resultArray.join('')
    const hashResut = createHash('sha1').update(resultString).digest('hex')
    const isWechatHost = hashResut === signature
    console.log('打印***hashResut', hashResut)
    console.log('打印***signature', signature)
    return { echostr, isWechatHost }
}

module.exports = {
    validateWechatHost
}